package com.zhiche.lisa.bms.service.utils.common;

import com.zhiche.lisa.bms.service.utils.LspUtil;
import com.zhiche.lisa.core.utils.Account.AccountUtil;
import com.zhiche.lisa.core.utils.Account.JwtAccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Author: liuanshun
 * @Description: 绑定creator属性
 * @Date: Create in 10:42 2018/9/13
 */
@Component
public class AccountBindUtil<T> {

    private static LspUtil lspUtil;

    private final String creatorColumn = "creator";
    private final String tenantIdColumn = "tenantId";

    @Autowired
    private void setLspUtil(LspUtil lspUtil) {
        AccountBindUtil.lspUtil = lspUtil;
    }

    public T bindCreatorAndTenantId(T type, boolean haveLsp) {
        //验证并绑定登陆信息
        JwtAccountVO jwtAccountVO = AccountUtil.getAccountInfoFromSecurityContext();
        if (Objects.nonNull(jwtAccountVO)) {
            new BeanColumnUtil().setColumn(type, creatorColumn, jwtAccountVO.getAccountId());
            if (haveLsp) {
                if (Objects.nonNull(jwtAccountVO.getTenantId())) {
                    new BeanColumnUtil().setColumn(type, tenantIdColumn, jwtAccountVO.getTenantId());
                }
            }
        }
        return type;
    }
}
